package com.chun.service.impl;

import com.chun.mapper.StudentMapper;
import com.chun.pojo.DegreeCountDTO;
import com.chun.pojo.PageBean;
import com.chun.pojo.Student;
import com.chun.pojo.StudentCountDTO;
import com.chun.service.StudentService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

    @Override
    public PageBean getStudents(String name, Integer degree, Integer clazzId, Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<Student> studentList = studentMapper.getStudents(name, degree, clazzId);
        Page<Student> studentPage = (Page<Student>) studentList;
        PageBean pageBean = new PageBean(studentPage.getTotal(), studentPage.getResult());
        return pageBean;
    }

    @Override
    public void deleteStudentByIds(Integer[] ids) {
        studentMapper.deleteStudentByIds(ids);
    }

    @Override
    public void addStudent(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.addStudent(student);
    }

    @Override
    public Student getStudentById(Integer id) {
        Student student = studentMapper.getStudentById(id);
        return student;
    }

    @Override
    public void updateStudent(Student student) {
        studentMapper.updateStudent(student);
    }

    @Override
    public void updateStudentScore(Integer id, Integer score) {
        studentMapper.updateStudentScore(id, score);
    }

    @Override
    public List<DegreeCountDTO> getStudentDegreeData() {
        List<DegreeCountDTO> degreeCountDTOList = studentMapper.getStudentDegreeData();
        return degreeCountDTOList;
    }

    @Override
    public List<StudentCountDTO> getStudentCountData() {
        List<StudentCountDTO> list = studentMapper.getStudentCountDTOList();
        return list;
    }
}
